#include "dockwidgetfilelist.h"
#include "ui_dockwidgetfilelist.h"
#include "dlgrename.h"
#include "qappwidget.h"

DockWidgetFileList::DockWidgetFileList(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::DockWidgetFileList),
    m_isSelectedItem(false),
    m_isSelectedRow(false),
    m_rowSelected(-1),
    m_hideColumns(true),
    m_parent(parent)
{
    ui->setupUi(this);
}

DockWidgetFileList::DockWidgetFileList(PFileList *fileList,QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::DockWidgetFileList),
    m_fileList(fileList),
    m_isSelectedItem(false),
    m_isSelectedRow(false),
    m_rowSelected(-1),
    m_hideColumns(true),
    m_parent(parent)
{
    ui->setupUi(this);
    m_filelist_model = new PFileListTableModel(m_fileList);
    ui->tableView_projectsModules->setModel(m_filelist_model);
    ui->tableView_projectsModules->resizeColumnsToContents();
    m_installListOfModules();

    m_vHeader = ui->tableView_projectsModules->verticalHeader();


    m_setupConnections();

    m_update();

    m_changeState(COLUMNS_HIDE);

}

void DockWidgetFileList::m_setupConnections()
{
    connect( m_vHeader ,
     SIGNAL( sectionClicked(int) ),this,
       SLOT( row_clicked_sl(int) )
            );

    connect( m_filelist_model,
    SIGNAL ( dataChanged(QModelIndex,QModelIndex)),this,
    SIGNAL ( fileListChanged())
             );
}

DockWidgetFileList::~DockWidgetFileList()
{
    delete ui;
}

void DockWidgetFileList::m_installListOfModules()
{
//    int i=0;
//    ExaModuleObj m = PFileItem::moduleObj(i);
//    while ( m != EXA_MODULE_OBJ_UNKNOWN )
//    {
//        ui->comboBox_modules->addItem(QSTR(PFileItem::moduleObjName(m)));
//        i=i+1;
//        m = PFileItem::moduleObj(i);
//    }

    QList<string> l = QList<string>::fromStdList(PFileItem::moduleListName());
    QStringList l_items;
    int i=0;
    for (i=0;i<l.size();i++)
    {
        l_items.push_back(QSTR(l.value(i)));
    }
    ui->comboBox_modules->addItems(l_items);
}

void DockWidgetFileList::on_toolButton_add_clicked()
{
    int i = ui->comboBox_modules->currentIndex();
    qDebug()<<"DockWidgetFileList::on_toolButton_add_clicked i = "<<i;
    ExaModuleObj m = PFileItem::moduleObj(i);
    m_filelist_model->append(m);
    ui->tableView_projectsModules->resizeColumnsToContents();
    m_update();
}

void DockWidgetFileList::on_toolButton_remove_clicked()
{
    QModelIndex i = ui->tableView_projectsModules->selectionModel()->selectedRows(0).at(0);
    m_filelist_model->removeRow(i.row());
    m_filelist_model->updateWEDChange();
    ui->tableView_projectsModules->clearSelection();
    m_isSelectedRow = false;
    m_update();
}

void DockWidgetFileList::on_toolButton_rename_clicked()
{
    QString descr = QSTR(m_fileList->valueAt(m_rowSelected).getDescr());
    QString key = QSTR(m_fileList->valueAt(m_rowSelected).getKeyStr());
    DlgRename* d_ui = new DlgRename(
                descr,
                this
                );
    if(d_ui->exec())
    {
        m_fileList->rename(m_rowSelected,key.toStdString(),d_ui->getDescr().toStdString());
        m_filelist_model->updateWEDChange();
    }
    ui->tableView_projectsModules->clearSelection();
    m_isSelectedRow = false;
    m_update();
    delete d_ui;
}

void DockWidgetFileList::on_tableView_projectsModules_clicked(const QModelIndex &index)
{
    QItemSelectionModel *selection = ui->tableView_projectsModules->selectionModel();
    if (m_isSelectedRow)
    {
        if (index.row() != m_rowSelected)
        {
            selection->select(index,QItemSelectionModel::Select | QItemSelectionModel::Rows);
            m_isSelectedRow = true;
            m_rowSelected = index.row();
        }
        else
        {
            selection->select(index,QItemSelectionModel::Clear | QItemSelectionModel::Rows);
            m_isSelectedRow = false;
            m_rowSelected = -1;
        }
        // ui->tableView_projectsModules->clearSelection();
    }
    else
    {
        selection->select(index,QItemSelectionModel::Select | QItemSelectionModel::Rows);
        m_isSelectedRow = true;
        m_rowSelected = index.row();
    }
    m_update();
}

void DockWidgetFileList::on_tableView_projectsModules_doubleClicked(const QModelIndex &index)
{

}

void DockWidgetFileList::row_clicked_sl(int logicalIndex)
{
    QItemSelectionModel *selection = ui->tableView_projectsModules->selectionModel();

    qDebug()<<"m_rowSelected = "<< logicalIndex;
    if (m_rowSelected == logicalIndex)
    {
        m_isSelectedRow = false;
        // selection->clearSelection();
        m_rowSelected = -1;
    }
    else
    {
        m_isSelectedRow = true;
        m_rowSelected = logicalIndex;
    }
    m_update();
}

void DockWidgetFileList::m_update()
{
    if (m_filelist_model->rowCount() == 0)
    {
        m_changeState(LIST_EMPTY);
    }
    else
    {
        m_changeState(LIST_OK);
    }

    if (m_hideColumns)
        m_changeState(COLUMNS_HIDE);
    else
        m_changeState(COLUMNS_SHOW);

    if (m_isSelectedRow == true)
    {
        m_changeState(MAYBE_REMOVE);
        if (m_fileList->isModuleCurrent(m_rowSelected))
        {
            m_changeState(MAYBE_RENAME);
            m_changeState(MAYBE_OPENDLG);
            m_changeState(CANNOTBE_CURRENT);
        }
        else
        {
            m_changeState(MAYBE_CURRENT);
            m_changeState(CANNOTBE_RENAME);
            m_changeState(CANNOTBE_OPENDLG);
        }
    }
    else
    {
        m_changeState(CANNOTBE_REMOVE);
        m_changeState(CANNOTBE_OPENDLG);
    }
}

void DockWidgetFileList::on_toolButton_hide_clicked()
{
    m_hideColumns = true;
    m_update();
}

void DockWidgetFileList::on_toolButton_show_clicked()
{
    m_hideColumns = false;
    m_update();
}

void DockWidgetFileList::on_toolButton_current_clicked()
{
    ExaModuleErr err;
    err = m_fileList->setModuleCurrent(m_rowSelected,true);
    qDebug()<<"Errore ExaModuleErr ="<<err;
    if (err != EXA_MODULE_ERR_OK)
    {
        QAppWidget::errMsgBox(err,m_fileList->getAt(m_rowSelected));
        return;
    }
    else
    {
        m_filelist_model->updateWEDChangeWReset();
        m_update();

        emit fileListChanged();
    }
}

void DockWidgetFileList::updateWEDChange(void)
{
    m_filelist_model->updateWEDChange();
    // ui->tableView_projectsModules->clearSelection();
    // m_isSelectedRow = false;
    m_update();
}

void DockWidgetFileList:: m_changeState (dlgSTATE s) const
{
        switch (s)
        {
        case COLUMNS_SHOW :
        {
            ui->tableView_projectsModules->showColumn(0);
            ui->tableView_projectsModules->showColumn(1);
            ui->tableView_projectsModules->showColumn(2);
            ui->tableView_projectsModules->resizeColumnsToContents();
            ui->toolButton_hide->setEnabled(true);
            ui->toolButton_show->setEnabled(false);
        }
        break;
        case COLUMNS_HIDE :
        {
            ui->tableView_projectsModules->hideColumn(0);
            ui->tableView_projectsModules->hideColumn(1);
            ui->tableView_projectsModules->hideColumn(2);
            ui->tableView_projectsModules->resizeColumnsToContents();
            ui->toolButton_hide->setEnabled(false);
            ui->toolButton_show->setEnabled(true);
        }
        break;
        case LIST_EMPTY :
        {
              ui->toolButton_hide->setEnabled(false);
              ui->toolButton_show->setEnabled(false);
            ui->toolButton_rename->setEnabled(false);
            ui->toolButton_remove->setEnabled(false);
           ui->toolButton_current->setEnabled(false);
        }
        break;
        case LIST_OK :
        {
            ui->toolButton_rename->setEnabled(false);
            ui->toolButton_remove->setEnabled(false);
           ui->toolButton_current->setEnabled(false);
        }
        break;
        case MAYBE_RENAME :
        {
             ui->toolButton_rename->setEnabled(true);
            ui->toolButton_current->setEnabled(true);
        }
        break;
        case CANNOTBE_RENAME :
        {
             ui->toolButton_rename->setEnabled(false);
            ui->toolButton_current->setEnabled(false);
        }
        case MAYBE_CURRENT :
        {
            ui->toolButton_current->setEnabled(true);
        }
        break;
        case CANNOTBE_CURRENT :
        {
            ui->toolButton_current->setEnabled(false);
        }
        break;
        case MAYBE_REMOVE :
        {
             ui->toolButton_remove->setEnabled(true);
        }
        break;
        case CANNOTBE_REMOVE :
        {
             ui->toolButton_remove->setEnabled(false);
        }
        break;
        case MAYBE_OPENDLG :
        {
             ui->toolButton_openModule->setEnabled(true);
        }
        break;
        case CANNOTBE_OPENDLG :
        {
             ui->toolButton_openModule->setEnabled(false);
        }
        break;
        default:
        break;
        };
}

void DockWidgetFileList::on_toolButton_openModule_clicked()
{
    // m_fileList->getAt(m_rowSelected).
    // Cerco di ottenere il gestore
    AbstractModule* mod;
    mod = AbstractModule::getModule(m_fileList->getAt(m_rowSelected).getModuleType());
    if (!mod)
    {
        return ;
    }
    // m_notifyMsg(tr("[Spettri]: apro modulo ... "));
    mod->dlgSetParent(m_parent);
    if(mod->dlgExec())
    {
        // m_notifyMsg(tr("[Spettri]: modificati !!!"));
        this->updateWEDChange();
        QItemSelectionModel* selection_model = ui->tableView_projectsModules->selectionModel();
        QModelIndex index = ui->tableView_projectsModules->model()->index(m_rowSelected,0);
        selection_model->select(index,QItemSelectionModel::Select | QItemSelectionModel::Rows);
    }
    else
    {
        // m_notifyMsg(tr("[Spettri]: annullata !!!"));
    }
    mod->dlgDelete();
}

void DockWidgetFileList::clearSelection(void)
{
    ui->tableView_projectsModules->clearSelection();
    m_isSelectedRow = false;
    m_rowSelected = -1;
}

//void DockWidgetFileList::on_DockWidgetFileList_fileListChanged()
//{
//    m_parent->update();
//}
